package com.edu.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.edu.dao.BaseDao;
import com.edu.dao.ChartingRecordsDao;
import com.edu.entity.ChartingRecords;
import com.edu.entity.User;

public class ChartingRecordsDaoImpl implements ChartingRecordsDao {
	private BaseDao bd1 = new BaseDao();
	@Override
	public int insertInto(ChartingRecords cr) {
		String sql = "INSERT INTO chartingrecords(sendUsername,reciveusername,message,send_at) VALUES(?,?,?,NOW())";
		Object[] obj = {cr.getSendUsername(),cr.getReciveusername(),cr.getMessage()};
		return bd1.executeUpdate(sql, obj);
	}
	@Override
	public List<ChartingRecords> selectChartingRecords(ChartingRecords cr) {
	    Connection conn = bd1.getConn();
	    StringBuilder sb = new StringBuilder();
	    List<ChartingRecords> crList = new ArrayList<>();
	    List<Object> objList = new ArrayList<>();
	    
	    // 使用正确的数据库字段名
	    sb.append("SELECT * FROM chartingrecords WHERE 1=1 ");
	    
	    if (cr.getSendUsername() != null && cr.getReciveusername() != null) {
	        sb.append(" AND (sendUsername = ? AND reciveusername = ?) OR (sendUsername = ? AND reciveusername = ?)");
	        objList.add(cr.getSendUsername());
	        objList.add(cr.getReciveusername());
	       
	        objList.add(cr.getReciveusername());
	        objList.add(cr.getSendUsername());
	    }


	    
	    String sql = sb.toString();
	    System.out.println(sql);
	    PreparedStatement ps = null;
	    ResultSet rs = null;
	    
	    try {
	        ps = conn.prepareStatement(sql);
	        for (int i = 0; i < objList.size(); i++) {
	            ps.setObject(i + 1, objList.get(i));
	        }
	        
	        rs = ps.executeQuery();
	        while (rs.next()) {
	            ChartingRecords cRecords = new ChartingRecords();
	            cRecords.setChartingRecords_id(rs.getInt("chartingRecords_id")); // 
	            cRecords.setSendUsername(rs.getString("sendUsername"));
	            cRecords.setReciveusername(rs.getString("reciveusername"));
	            cRecords.setSend_at(rs.getTimestamp("send_at")); // 使用Timestamp
	            cRecords.setMessage(rs.getString("message"));
	            crList.add(cRecords);
	        }
//	        System.out.println("***********");
//	        System.out.println(crList);
//	        System.out.println("&&&&&&&&&&&");
	        return crList;
	    } catch (SQLException e) {
	        e.printStackTrace();
	        return Collections.emptyList();
	    } finally {
	        bd1.closeAll(conn, ps, rs);
	    }
	}
	@Override
	public List<ChartingRecords> selectMyChartingObjs(String sendUsername) {
	    Connection conn = bd1.getConn();
	    StringBuilder sb = new StringBuilder();
	    List<ChartingRecords> crList = new ArrayList<>();
	    
	    // 使用正确的数据库字段名
	    String sql = "SELECT  * FROM chartingRecords WHERE sendUsername = ? OR reciveusername = ?";
	    

	    PreparedStatement ps = null;
	    ResultSet rs = null;
	    
	    try {
	        ps = conn.prepareStatement(sql);
	        ps.setString(1, sendUsername);
	        ps.setString(2, sendUsername);
	        rs = ps.executeQuery();
	        while (rs.next()) {
	            ChartingRecords cRecords = new ChartingRecords();
	            cRecords.setChartingRecords_id(rs.getInt("chartingRecords_id")); // 
	            cRecords.setSendUsername(rs.getString("sendUsername"));
	            cRecords.setReciveusername(rs.getString("reciveusername"));
	            cRecords.setSend_at(rs.getTimestamp("send_at")); // 使用Timestamp
	            cRecords.setMessage(rs.getString("message"));
	            crList.add(cRecords);
	        }
//	        System.out.println("***********");
//	        System.out.println(crList);
//	        System.out.println("&&&&&&&&&&&");
	        return crList;
	    } catch (SQLException e) {
	        e.printStackTrace();
	        return Collections.emptyList();
	    } finally {
	        bd1.closeAll(conn, ps, rs);
	    }
	}

}
